package com.dxyxx.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import br.com.pierry.simpletoast.SimpleToast;

import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.framework.BaseActivity;
import com.dxyxx.framework.BasePayActivity;
import com.dxyxx.framework.FragmentMainActivity;
import com.dxyxx.framework.GroupOrderListActivity;
import com.dxyxx.framework.OrderListActivity;
import com.dxyxx.framework.TicketActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 特别注意：
 * 
 * 1.可以弹出微信支付的界面，但是支付之后没有进入回调的Activity：WXPayEntryActivity ?
 * 
 * 微信开放平台有个
 * 不成文的规定（文档里没有说明），就是回调的Activity必须是：你的包名（微信demo里是：net.sourceforge.simcpux
 * ）+.wxapi.WXPayEntryActivity.java
 * 其他的接口回调的Activity必须是：你的包名（微信demo里是：net.sourceforge
 * .simcpux）+.wxapi.WXEntryActivity.java
 * 
 * @author Administrator
 * 
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

	private IWXAPI api;
	private TextView tv;
	private Button back;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_result);
		tv = (TextView) findViewById(R.id.word);
		back = (Button) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		api = WXAPIFactory.createWXAPI(this, "wxa207119c4b14badc");
		api.handleIntent(getIntent(), this);

	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		String s = "";
		String ss = "";
		s = ss + "";
	}

	@Override
	public void onResp(BaseResp resp) {
		//		resp.errCode== 0 ：表示支付成功
		//				resp.errCode== -1 ：表示支付失败
		//				resp.errCode== -2 ：表示取消支付
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		if (resp.errCode == 0) {
			SimpleToast.ok(WXPayEntryActivity.this, "支付成功");
		} else {
			SimpleToast.info(WXPayEntryActivity.this, "支付失败");
			tv.setText("您取消了支付");

		}
		//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
		//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//			builder.setTitle("提示");
		//			builder.setMessage(getString(R.string.pay_result_callback_msg, resp.errStr + ";code=" + String.valueOf(resp.errCode)));
		//			builder.show();
		//		}
	}

	@Override
	public void finish() {
		userPreferences = UserPreferences.getInstance(getApplicationContext());
		if ("o".equals(userPreferences.getPayType())) {
			startActivity(new Intent(WXPayEntryActivity.this, OrderListActivity.class));
		} else if ("t".equals(userPreferences.getPayType())) {
			startActivity(new Intent(WXPayEntryActivity.this, TicketActivity.class));
		} else {//gg
			startActivity(new Intent(WXPayEntryActivity.this, GroupOrderListActivity.class));
		}
		super.finish();
	}
}